package com.qs.questionnairesurveysystem.controller;

import com.qs.questionnairesurveysystem.dto.LoginRequest;
import com.qs.questionnairesurveysystem.dto.Result;
import com.qs.questionnairesurveysystem.entity.Admin;
import com.qs.questionnairesurveysystem.entity.JwtUser;
import com.qs.questionnairesurveysystem.entity.User;
import com.qs.questionnairesurveysystem.service.AdminService;
import com.qs.questionnairesurveysystem.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponses;
import io.swagger.annotations.ApiResponse;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/auth")
@Api(tags = "Authentication Controller")
public class AuthController {

    @Autowired
    private UserService userService;

    @Autowired
    private AdminService adminService;

    @ApiOperation(value = "Admin Login", notes = "Authenticate an admin user and generate a JWT token.")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "Login successful, JWT token returned"),
            @ApiResponse(code = 401, message = "Invalid username or password")
    })
    @PostMapping("/login")
    @ResponseBody
    public Result<JwtUser> login(@RequestBody LoginRequest loginRequest) {
        JwtUser res = new JwtUser();
        if(loginRequest.getType().equals("admin")) {
            Admin admin = adminService.getAdminByName(loginRequest.getUsername());
            if(admin != null && admin.getPassword().equals(loginRequest.getPassword())) {
                res.setUsername(admin.getName());
                res.setType(loginRequest.getType());
                res.setId(admin.getAdminId());
                return Result.success(res);
            }

        }else {
            User user = userService.getUserByUsername(loginRequest.getUsername());
            if(user != null && user.getPassword().equals(loginRequest.getPassword())) {
                res.setUsername(user.getUsername());
                res.setType(loginRequest.getType());
                res.setId(user.getUserId());
                return Result.success(res);
            }
        }

        return Result.error("用户名或者密码错误", 405);
    }

    @PostMapping("/register")
    @ResponseBody
    public Result<User> register(@RequestBody User registerRequest) {
        // Check if username already exists
        User existingUser = userService.getUserByUsername(registerRequest.getUsername());
        if (existingUser != null) {
            return Result.error("用户名已存在", 400);
        }

        User u = userService.getUserByUsername(registerRequest.getUsername());
        if(u != null) {
            return Result.error("用户名已存在", 403);
        }
        userService.createUser(registerRequest);
        return Result.success(registerRequest);
    }

    @GetMapping("/admin/profile")
    @ResponseBody
    public Result<Admin> adminProfile(HttpServletRequest request) {
        String username = request.getHeader("Authorization");

        Admin admin = adminService.getAdminByName(username);
        if(admin == null) {
            return Result.error("not found", 404);
        }
        return Result.success(admin);
    }

    @GetMapping("/user/profile")
    @ResponseBody
    public Result<User> userProfile(HttpServletRequest request) {
        String username = request.getHeader("Authorization");

        User user = userService.getUserByUsername(username);
        if(user == null) {
            return Result.error("not found", 404);
        }
        return Result.success(user);
    }
}
